package com.cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.cn.domain.User;
import com.cn.service.UserService;
import com.cn.service.impl.UserServiceImpl;

@WebServlet("/CheckUserServlet")
public class CheckUserServlet extends HttpServlet {
	private UserService userService=new UserServiceImpl();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user=null;
		//获取响应的操作
		PrintWriter pw=resp.getWriter();
		String type=req.getParameter("type");
		if("checkExist".equals(type)) {
			String userName=req.getParameter("userName");
			user=userService.queryUserByKey("HU_USER_NAME", userName);
			if(user!=null) {//已经存在,不能注册
				pw.println("1");
			}else {
				pw.println("100");
			}
			//
		}else if("checkValidateCode".equals(type)) {
			String veryCode=req.getParameter("veryCode");
			//先验证验证码
			String validateCode=(String) req.getSession().getAttribute("validateCode");
			if(validateCode.equals(veryCode)) {
				//成功可以验证
				pw.println("1");
			}else {
				pw.println("100");
			}
		}else if("emailExist".equals(type)) {
			String email=req.getParameter("email");
			//System.out.println("type=Emial="+email);
			user=userService.queryUserByKey("HU_EMAIL", email);
			if(user!=null) {//已经存在,不能注册
				pw.println("1");
			}else {
				pw.println("100");
			}
		}else if("mobileTel".equals(type)) {
			String tel=req.getParameter("mobile");
			user=userService.queryUserByKey("HU_MOBILE", tel);
			if(user!=null) {//已经存在,不能注册
				pw.println("1");
			}else {
				pw.println("100");
			}
		}else if("identityCheck".equals(type)) {
			String identity=req.getParameter("identity");
			user=userService.queryUserByKey("HU_IDENTITY_CODE", identity);
			if(user!=null) {//已经存在,不能注册
				pw.println("1");
			}else {
				pw.println("100");
			}
		}
	}
}
